﻿using System.ComponentModel;
using System.Windows;
using VisionDesigner.BlobFind;
using VM.Common.Enums;
using VM.Common.Helper;

namespace Plugin.Blob
{
    [Serializable]
    public class ParamModel : NotifyPropertyBase
    {

        public Dictionary<string, string> ParamMap = new Dictionary<string, string>
        {
            { "不进行二值化", "BinaryImage" },
            { "单阈值", "SingleThreshold" },
            { "双阈值", "DoubleThreshold" },
            { "自动阈值", "AutoThreshold" },
            { "软阈值(固定)", "FixedSoftThreshold" },
            { "软阈值(相对)", "RelativeSoftThreshold" },
            { "亮于背景", "DarkOnBright" },
            { "暗于背景", "BrightOnDark" },
            { "8连通", "EightConnected" },
            { "4连通", "FourConnected" },
        };

        // 阈值方式
        private List<string> _ThresholdTypeArray = EnumHelper.GetEnumDescriptions<ThresholdTypes>();
        public List<string> ThresholdTypeArray
        {
            get { return _ThresholdTypeArray; }
            set { Set(ref _ThresholdTypeArray, value); }
        }

        private string _ThresholdType = EnumHelper.GetEnumDescription(ThresholdTypes.SingleThreshold);
        public string ThresholdType
        {
            get { return _ThresholdType; }
            set
            {
                Set(ref _ThresholdType, value);
                UpdateThresholdVisible();
            }
        }

        private Visibility _LowThresholdVisible = Visibility.Visible;
        public Visibility LowThresholdVisible
        {
            get { return _LowThresholdVisible; }
            set { Set(ref _LowThresholdVisible, value); }
        }

        private Visibility _HightThresholdVisible = Visibility.Collapsed;
        public Visibility HightThresholdVisible
        {
            get { return _HightThresholdVisible; }
            set { Set(ref _HightThresholdVisible, value); }
        }

        private Visibility _SoftThresholdVisible = Visibility.Collapsed;
        public Visibility SoftThresholdVisible
        {
            get { return _SoftThresholdVisible; }
            set { Set(ref _SoftThresholdVisible, value); }
        }

        private Visibility _SoftnessVisible = Visibility.Collapsed;
        public Visibility SoftnessVisible
        {
            get { return _SoftnessVisible; }
            set { Set(ref _SoftnessVisible, value); }
        }

        private Visibility _SoftRatioVisible = Visibility.Collapsed;
        public Visibility SoftRatioVisible
        {
            get { return _SoftRatioVisible; }
            set { Set(ref _SoftRatioVisible, value); }
        }

        // 连通类型
        private List<string> _ConnectivityArray = EnumHelper.GetEnumDescriptions<Connectivitys>();
        public List<string> ConnectivityArray
        {
            get { return _ConnectivityArray; }
            set { Set(ref _ConnectivityArray, value); }
        }

        private string _Connectivity = EnumHelper.GetEnumDescription(Connectivitys.EightConnected);
        public string Connectivity
        {
            get { return _Connectivity; }
            set { Set(ref _Connectivity, value); }
        }

        private int _Minimum = 1;
        public int Minimum
        {
            get { return _Minimum; }
            set { Set(ref _Minimum, value); }
        }

        private int _Maximum = 999999999;
        public int Maximum
        {
            get { return _Maximum; }
            set { Set(ref _Maximum, value); }
        }

        // 整型
        private Dictionary<string, int> _paramInt = new Dictionary<string, int>();
        public Dictionary<string, int> ParamInt
        {
            get { return _paramInt; }
            set { Set(ref _paramInt, value); }
        }

        // Array
        private Dictionary<string, Array> _paramArray = new Dictionary<string, Array>();
        public Dictionary<string, Array> ParamArray
        {
            get { return _paramArray; }
            set { Set(ref _paramArray, value); }
        }

        private Dictionary<string, object> _paramEnum = new Dictionary<string, object>();
        public Dictionary<string, object> ParamEnum
        {
            get { return _paramEnum; }
            set { Set(ref _paramEnum, value); }
        }


        public ParamModel()
        {
            // 整型数据
            ParamInt.Add("LowThreshold", 100);
            ParamInt.Add("HightThreshold", 150);
            ParamInt.Add("FindNum", 1);
            ParamInt.Add("HoleMinArea", 100);
            ParamInt.Add("MinArea", 1);
            ParamInt.Add("MaxArea", 999999999);
            ParamInt.Add("LowSoftThreshold", 80);
            ParamInt.Add("HightSoftThreshold", 128);
            ParamInt.Add("Softness", 0);
            ParamInt.Add("SoftLeftRatio", 5);
            ParamInt.Add("SoftRightRatio", 5);
            ParamInt.Add("SoftLowRatio", 40);
            ParamInt.Add("SoftHighRatio", 60);
            //  Enum数据
            ParamArray.Add("Polarity", Enum.GetValues(typeof(Polaritys)));
            ParamEnum.Add("Polarity", Polaritys.亮于背景);

        }

        public void UpdateParam(CBlobFindTool cBlobFindTool)
        {
            // 阈值方式
            cBlobFindTool.SetRunParam("ThresholdType", ParamMap[ThresholdType]);
            // 连通类型
            cBlobFindTool.SetRunParam("connectivity", ParamMap[Connectivity]);
            // 整型数据
            foreach (var pair in ParamInt)
            {
                string key = pair.Key;
                string value = pair.Value.ToString();
                cBlobFindTool.SetRunParam(key, value);
            }
            // Enum数据
            foreach (var pair in ParamEnum)
            {
                string key = pair.Key;
                string value = pair.Value.ToString();
                if (ParamMap.ContainsKey(value))
                {
                    cBlobFindTool.SetRunParam(key, ParamMap[value]);
                }
            }
        }

        private void UpdateThresholdVisible()
        {
            switch (ThresholdType)
            {
                case "不进行二值化":
                    LowThresholdVisible = Visibility.Collapsed;
                    HightThresholdVisible = Visibility.Collapsed;
                    SoftThresholdVisible = Visibility.Collapsed;
                    SoftnessVisible = Visibility.Collapsed;
                    SoftRatioVisible = Visibility.Collapsed;
                    break;
                case "单阈值":
                    LowThresholdVisible = Visibility.Visible;
                    HightThresholdVisible = Visibility.Collapsed;
                    SoftThresholdVisible = Visibility.Collapsed;
                    SoftnessVisible = Visibility.Collapsed;
                    SoftRatioVisible = Visibility.Collapsed;
                    break;
                case "双阈值":
                    LowThresholdVisible = Visibility.Visible;
                    HightThresholdVisible = Visibility.Visible;
                    SoftThresholdVisible = Visibility.Collapsed;
                    SoftnessVisible = Visibility.Collapsed;
                    SoftRatioVisible = Visibility.Collapsed;
                    break;
                case "自动阈值":
                    LowThresholdVisible = Visibility.Collapsed;
                    HightThresholdVisible = Visibility.Collapsed;
                    SoftThresholdVisible = Visibility.Collapsed;
                    SoftnessVisible = Visibility.Collapsed;
                    SoftRatioVisible = Visibility.Collapsed;
                    break;
                case "软阈值(固定)":
                    LowThresholdVisible = Visibility.Collapsed;
                    HightThresholdVisible = Visibility.Collapsed;
                    SoftThresholdVisible = Visibility.Visible;
                    SoftnessVisible = Visibility.Visible;
                    SoftRatioVisible = Visibility.Collapsed;
                    break;
                case "软阈值(相对)":
                    LowThresholdVisible = Visibility.Collapsed;
                    HightThresholdVisible = Visibility.Collapsed;
                    SoftThresholdVisible = Visibility.Collapsed;
                    SoftnessVisible = Visibility.Visible;
                    SoftRatioVisible = Visibility.Visible;
                    break;
                default:
                    break;
            }
        }
    }

    /// <summary>
    /// 输出图像
    /// </summary>
    [Serializable]
    public enum OutImageTypes
    {
        原图,
        二值化图像,
        Blob图像
    }

    /// <summary>
    /// 阈值方式
    /// </summary>
    [Serializable]
    public enum ThresholdTypes
    {
        [Description("不进行二值化")]
        BinaryImage,

        [Description("单阈值")]
        SingleThreshold,

        [Description("双阈值")]
        DoubleThreshold,

        [Description("自动阈值")]
        AutoThreshold,

        [Description("软阈值(固定)")]
        SoftThresholdFixed,

        [Description("软阈值(相对)")]
        RelativeSoftThreshold
    }

    /// <summary>
    /// 极性
    /// </summary>
    [Serializable]
    public enum Polaritys
    {
        亮于背景,
        暗于背景
    }

    /// <summary>
    /// 连通性
    /// </summary>
    [Serializable]
    public enum Connectivitys
    {
        [Description("8连通")]
        EightConnected,

        [Description("4连通")]
        FourConnected,
    }

}
